import util from '../../utils/util';
var app = getApp()
var myhost = app.globalData.myhost

// pages/books/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    bang: [{
        list: ["医判", "步步惊心", "末世之黎明救赎 ", "纨扇新裁", "一世倾城"],
        type: myhost + "/img/pic/月票.jpg"
      },
      {
        list: ["乐队的盛夏", "心照日月", "神医弃女", "温血动物", "这个帝君超难哄"],
        type: myhost + "/img/pic/打赏.jpg"
      },
      {
        list: ["最好的我们", "长歌落日圆", "农门长姐有空间", "亲爱的，二进制", "锦绣鱼图"],
        type: myhost + "/img/pic/推荐.jpg"
      },
      {
        list: ["特别的归乡者", "左耳", "暗恋·橘生淮南", "冥冥之中喜欢你", "你从时光来"],
        type: myhost + "/img/pic/更新.jpg"
      },
      {
        list: ["深空彼岸", "银狐", "嫁皇叔", "贵女她金屋藏夫", "有婚可乘"],
        type: myhost + "/img/pic/畅销.jpg"
      },
    ],
    lunbo: [],
    todaylist: [],
    reshu: [],
    reshumore: [],
    categories: [],
    newbooks: [],
    reputation: [],
    reputationmore:[],
    recommendindex: ['1', '2', '3', '4', '8'],
    recommend: [],
    love: []
  },
  newchange: function () {
    wx.showLoading({
      title: '加载中',
      mark: true
    });
    //加载重磅新书
    var listnew = []
    wx.request({
      url: myhost + '/book/getrandombookid',
      success: (res) => {
        console.log('换一换', res.data)
        for (var i = 0; i < 8; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i],
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              listnew.push(res.data)
            }
          })
        }
        setTimeout(() => {
          this.setData({
            newbooks: listnew
          })
        }, 1000)
      }
    })
    wx.hideLoading();
  },
  newchange2: function () {
    wx.showLoading({
      title: '加载中',
      mark: true
    });
    //加载重磅新书
    var listnew = []
    wx.request({
      url: myhost + '/book/getrandombookid',
      success: (res) => {
        console.log('换一换', res.data)
        for (var i = 0; i < 8; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i],
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              listnew.push(res.data)
            }
          })
        }
        setTimeout(() => {
          this.setData({
            love: listnew
          })
        }, 1000)
      }
    })
    wx.hideLoading();
  },
  myinit: function (index) {
    if (index == 3 && index == 2) {
      return
    }
    //加载轮播图
    wx.request({
      url: myhost + '/getrandompic?sex=' + index,
      success: (res) => {
        // console.log('pic',res.data)
        this.setData({
          lunbo: res.data
        })
      }
    })
    //加载今日热书
    wx.request({
      url: myhost + '/book/getbookidbyreadnum?sex=' + index,
      success: (res) => {
        // console.log('所有今日热书',res.data)
        var listall = []
        for (var i = 0; i < res.data.length; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i],
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              listall.push(res.data)
            }
          })
        }
        setTimeout(() => {
          this.setData({
            reshumore: listall
          })
        }, 1000)

        wx.request({
          url: myhost + '/book/detail?bookId=' + res.data[0],
          success: (res) => {
            // console.log(res.data)
            res.data.pic = myhost + res.data.pic
            this.setData({
              todaylist: [res.data]
            })
          }
        })

        var list = []
        for (var i = 1; i < 5; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i],
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              list.push(res.data)
            }
          })
        }
        setTimeout(() => {
          this.setData({
            reshu: list
          })
          // console.log('list',list)
        }, 1000)
      }
    })
    //加载分类
    wx.request({
      url: myhost + '/liu/OfSex?sex=' + index,
      success: (res) => {
        // console.log(res.data)
        this.setData({
          categories: res.data
        })
      }
    })
    //加载重磅新书
    var listnew = []
    wx.request({
      url: myhost + '/book/getlatestbookid?sex=' + index,
      success: (res) => {
        for (var i = 0; i < 8; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i],
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              listnew.push(res.data)
            }
          })
        }
        setTimeout(() => {
          this.setData({
            newbooks: listnew
          })
        }, 1000)
      }
    })
    //加载口碑佳作
    var listkoubei = []
    wx.request({
      url: myhost + '/book/getbookidbycommentnum?sex=' + index,
      success: (res) => {
        for (var i = 0; i < res.data.length; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i],
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              listkoubei.push(res.data)
            }
          })
        }
        setTimeout(() => {
          this.setData({
            reputation: listkoubei.slice(0,6),
            reputationmore:listkoubei
          })
        }, 1000)
      }
    })
    // console.log('reputation',this.data.reputation)
    //加载主编推荐
    var listre = []
    for (var myid of this.data.recommendindex) {
      wx.request({
        url: myhost + '/book/detail?bookId=' + myid,
        success: (res) => {
          // console.log(res.data)
          res.data.pic = myhost + res.data.pic
          listre.push(res.data)
        }
      })
    }
    setTimeout(() => {
      this.setData({
        recommend: listre
      })
    }, 1000)
    // 加载猜你喜欢
    var lovelist = []
    wx.request({
      url: myhost + 'book/getrandombookid',
      success: (res) => {
        for (var i = 0; i < res.data.length; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i],
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              lovelist.push(res.data)
            }
          })
        }
        setTimeout(() => {
          this.setData({
            love: lovelist
          })
        }, 1000)
      }
    })
  },
  // 顶部栏参数传回
  backindex(e) {
    this.setData({
      index: e.detail.index
    })
    this.myinit(this.data.index)
  },
  //书籍详情
  detail: function (e) {
    var page = e.currentTarget.dataset.page
    var bookid = e.currentTarget.dataset.bookid
    // console.log(e.currentTarget)
    if (page) {
      wx.navigateTo({
        url: page + '?bookid=' + bookid,
      })
    }
  },
  //更多
  showMoreBook: function (e) {
    console.log(e.currentTarget.dataset)
    var page = e.currentTarget.dataset.page;
    var title = e.currentTarget.dataset.title;
    var list = e.currentTarget.dataset.list;
    console.log(list)
    if (page) {
      wx.navigateTo({
        url: page + '?title=' + title + '&list=' + JSON.stringify(list),
      })
    }
  },
  morecategories: function (e) {
    wx.switchTab({
      url: '/pages/category/index',
    })
  },
  morekoubei: function (e) {
    console.log(e.currentTarget.dataset)
    var page = e.currentTarget.dataset.page;
    var title = e.currentTarget.dataset.title;
    var list = e.currentTarget.dataset.list;
    console.log(list)
    if (page) {
      wx.navigateTo({
        url: page+'?title=' + title + '&list=' + JSON.stringify(list),
      })
    }
  },
  morezhubian: function (e) {
    console.log(e.currentTarget.dataset)
    var page = e.currentTarget.dataset.page;
    var title = e.currentTarget.dataset.title;
    var list = e.currentTarget.dataset.list;
    console.log(list)
    if (page) {
      wx.navigateTo({
        url: page+'?title=' + title + '&list=' + JSON.stringify(list),
      })
    }
  },
  category: function (e) {
    var categoty=e.currentTarget.dataset.category
    // console.log(categoty)
    wx.request({
      url: myhost+'/liu/IdOfStyle?style='+categoty,
      success:(res)=>{
        console.log(res.data)
        for(var i=0;i<res.data.length;i++){
          res.data[i].pic=myhost+res.data[i].pic
        }
        wx.navigateTo({
          url: '/pages/list/list?title=' + categoty + '&list=' + JSON.stringify(res.data),
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.myinit(0)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // console.log(this.data.todaylist)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})